package com.aiti.lulian.service.impl;

import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.lulian.constant.GxmhConstant;
import com.aiti.lulian.dto.gonxin.FundApplicationDto;
import com.aiti.lulian.entity.FundApplication;
import com.aiti.lulian.gxmh.dto.SysUserDto;
import com.aiti.lulian.mapper.gongxin.FundApplicationMapper;
import com.aiti.lulian.mapper.gxmh.GxmhSysMapper;
import com.aiti.lulian.service.IFundApplicationService;
import com.aiti.lulian.vo.gongxin.FundApplicationVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FundApplicationServiceImpl extends BaseSerivceImpl<FundApplicationMapper, FundApplication> implements IFundApplicationService {

    @Autowired
    private FundApplicationMapper fundApplicationMapper;
    @Autowired
    private GxmhSysMapper gxmhSysMapper;

    @Override
    public void submitFundApplication(FundApplicationDto dto) {
        FundApplication fundApplication = new FundApplication();
        BeanUtils.copyProperties(dto, fundApplication);
        fundApplicationMapper.insert(fundApplication);
    }

    @Override
    public List<FundApplicationVo> getApplicationList(Page<FundApplicationVo> page, FundApplicationDto dto) {
        SysUserDto sysUserDto = gxmhSysMapper.queryUserInfo(dto.getUserId());
        if(sysUserDto == null) {
            throw new BusinessException("用户不存在");
        }
        // 如果当前用户是服务商用户，则只能查看跟服务商相关的产品数据。目前一个服务商用户只有一条商品数据
        if(GxmhConstant.GXMH_SERVER_USER_FLAG.equals(sysUserDto.getSource())) {
            return fundApplicationMapper.selectServerUserApplicationList(page, dto);
        }
        return fundApplicationMapper.selectApplicationList(page, dto);
    }

    @Override
    public FundApplicationVo getApplicationDetails(String baseId) {
        return fundApplicationMapper.selectApplicationDetails(baseId);
    }

    @Override
    public void deleteFundApplication(FundApplicationDto dto) {
        fundApplicationMapper.deleteById(dto.getBaseId());
    }
}
